// Escreve no lcd o valor do ADC tratando o numero certo de casas decimais
void write_lcd(long input, int lin, int col)
{
int ch;
long value;

if (input >= 0) {
  value = input;
  }
  else {
    value = ~input + 1;
    }

ch = value / 1000;                  // extract volts digit

if ( input >= 0 )
  LCD_Chr( lin, col, '+' );         // write ASCII digit at 2nd row, 9th column
  else LCD_Chr( lin, col, '-' );    // write ASCII digit at 2nd row, 9th column

LCD_Chr_CP( 48+ch );                // write ASCII digit at 2nd row, 9th column
LCD_Chr_CP('.');

ch = ( value / 100 ) % 10;          // extract 0.1 volts digit
LCD_Chr_CP( 48 + ch );              // write ASCII digit at cursor point

ch = ( value / 10 ) % 10;           // extract 0.01 volts digit
LCD_Chr_CP( 48 + ch );              // write ASCII digit at cursor point

ch = value % 10;                    // extract 0.001 volts digit
LCD_Chr_CP( 48 + ch );              // write ASCII digit at cursor point
}

// Limpa marcacoes de selecao dos canais
void clean_marks() {
  LCD_Chr( 1, 8, ' ' );
  LCD_Chr( 2, 8, ' ' );
}

int testa_uart() {
  UART1_Write_Text("ok");
  UART1_Write(10);                     // LF
  UART1_Write(13);                     // CR
return 0;
}

unsigned long media(unsigned char channel, unsigned char qty)
{
  unsigned int vlMedia = 0;
  unsigned char i;
  
  for(i=0; i<qty; i++) {
    vlMedia += ADC_Read(channel);
  }
  return vlMedia/qty;
}